/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.iec.edp.caf.rpc.remote.http;

import io.grpc.ManagedChannelBuilder;
import io.iec.edp.caf.common.JSONSerializer;
import io.iec.edp.caf.logging.CommonConstant;
import io.iec.edp.caf.rest.client.RESTProxyClientFactory;
import io.iec.edp.caf.rpc.api.channel.RpcAbstractChannel;
import io.iec.edp.caf.rpc.api.channel.RpcChanelAsynCallBack;
import io.iec.edp.caf.rpc.api.channel.RpcChannel;
import io.iec.edp.caf.rpc.api.common.RpcChannelType;
import io.iec.edp.caf.rpc.api.grpc.GrpcInvokeServiceGrpc;
import io.iec.edp.caf.rpc.api.serialize.RpcSerializeUtil;
import io.iec.edp.caf.rpc.api.support.ConstanceVarible;
import io.iec.edp.caf.rpc.api.support.RpcTimeoutHolder;
import io.iec.edp.caf.rpc.remote.http.api.RpcServiceApi;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.apache.commons.lang3.NotImplementedException;
import org.apache.cxf.jaxrs.client.Client;
import org.apache.cxf.jaxrs.client.WebClient;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

@Slf4j
public class HttpChanel extends RpcAbstractChannel {

    private Client webClient;

    private RpcServiceApi service;

    public HttpChanel(){
        super();
    }

    @SneakyThrows
    private HttpChanel(URL url2,String filterType) {
        this.filterType = filterType;
        this.host = url2.getHost();
        this.port = url2.getPort();
        var remoteUrl = url2.toString();
        URL url = new URL(String.format("%s%s", remoteUrl, ConstanceVarible.RPC_HTTP_BASE_PATH));
        service = RESTProxyClientFactory.build(RpcServiceApi.class, url);
        webClient = WebClient.client(service);
    }

    @Override
    public String channelType() {
        return RpcChannelType.REST.getValue();
    }

    @Override
    public RpcChannel buildChannel(String host, int port) {
        throw new NotImplementedException("http chanel not support this method");
    }

    @Override
    public RpcChannel buildChannel(String host, int port, String filterType) {
        throw new NotImplementedException("http chanel not support this method");
    }

    @Override
    public RpcChannel buildChannel(URL url, String filterType) {

        return new HttpChanel(url, filterType);
    }

    @Override
    public void addHeaders(Map<String, String> context) {

    }

    @Override
    public void startServer(Map<String, Object> args) {
        log.info("http chanel start server");
    }

    @Override
    protected void stopServer() {
        log.info("http chanel stop server");
    }

    @Override
    public void startClient() {
        log.info("http chanel start server");
    }

    @Override
    public void stopClient() {
        log.info("http chanel stop client");
    }

    @Override
    public void serviceRegister() {
        super.serviceRegister();
    }

    @Override
    public String invokeRemoteService(String serviceId, String version, LinkedHashMap<String, String> parameters, HashMap<String, String> context) throws IllegalAccessException, ClassNotFoundException, InstantiationException, IOException, NoSuchMethodException, InvocationTargetException {
        String result = service.invoke(serviceId, "1.0", (LinkedHashMap<String, String>) parameters);
        return result;
    }

    @Override
    public InputStream invokeRemoteServiceStream(String serviceId, String version, LinkedHashMap<String, String> parameters, HashMap<String, String> context) throws IllegalAccessException, ClassNotFoundException, InstantiationException, IOException, NoSuchMethodException, InvocationTargetException {
        service.invokeStream(serviceId, version, parameters);
        var response = webClient.getResponse();
        InputStream result = response.readEntity(InputStream.class);
        return result;
    }

    @Override
    public void invokeRemoteServiceStreamAsyn(String serviceId, String version, LinkedHashMap<String, String> parameters, HashMap<String, String> context, RpcChanelAsynCallBack callback) throws IllegalAccessException, ClassNotFoundException, InstantiationException, IOException, NoSuchMethodException, InvocationTargetException {
        throw new NotImplementedException("http chanel not support this method");
    }

    @Override
    public String invokeRemoteServiceStream(InputStream inputStream, String serviceId, String version, LinkedHashMap<String, String> parameters, HashMap<String, String> context) throws IllegalAccessException, ClassNotFoundException, InstantiationException, IOException, NoSuchMethodException, InvocationTargetException {
        var result = service.invokeStream(serviceId, "1.0", parameters, inputStream);
        return "";
    }
}
